home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Gauge.java < prev    next >
Text File  |  1998-09-15  |  2KB  |  81 lines

  1. /*
  2.  * @(#)Gauge.java    1.1 98/07/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package actual; 
  9. import java.awt.*;
  10. import java.applet.*;
  11.  
  12. /*
  13.  * Gauge - a class that implements a lightweight component
  14.  * that can be used, for example, as a performance meter.
  15.  *
  16.  * Lightweight components can have "transparent" areas, meaning that
  17.  * you can see the background of the container behind these areas.
  18.  *
  19.  */
  20. public class Gauge extends Component {
  21.     
  22.   // the current and total amounts that the gauge reperesents
  23.   int current = 0;
  24.   int total = 100;
  25.  
  26.   // The preferred size of the gauge
  27.   int Height = 18;   // looks good
  28.   int Width  = 250;  // arbitrary 
  29.  
  30.   /**
  31.    * Constructs a Gauge
  32.    */
  33.   public Gauge() {
  34.       this(Color.lightGray);
  35.   }
  36.  
  37.   /**
  38.    * Constructs a that will be drawn uses the
  39.    * specified color.
  40.    *
  41.    * @gaugeColor the color of this Gauge
  42.    */
  43.   public Gauge(Color gaugeColor) {
  44.       setBackground(gaugeColor);
  45.   }
  46.  
  47.   public void paint(Graphics g) {
  48.       int barWidth = (int) (((float)current/(float)total) * getSize().width);
  49.       g.setColor(getBackground());
  50.       g.fill3DRect(0, 0, barWidth, getSize().height-2, true);
  51.   }
  52.  
  53.   public void setCurrentAmount(int Amount) {
  54.       current = Amount; 
  55.  
  56.       // make sure we don't go over total
  57.       if(current > 100)
  58.        current = 100;
  59.  
  60.       repaint();
  61.   }
  62.  
  63.   public int getCurrentAmount() {
  64.       return current;
  65.   }
  66.  
  67.   public int getTotalAmount() {
  68.       return total;
  69.   }
  70.  
  71.   public Dimension getPreferredSize() {
  72.       return new Dimension(Width, Height);
  73.   }
  74.  
  75.   public Dimension getMinimumSize() {
  76.       return new Dimension(Width, Height);
  77.   }
  78.  
  79. }
  80.  
  81.